<?php
namespace App\Model;

use PhalApi\Model\NotORMModel as NotORM;
use App\Domain\Vip as Domain_Vip;


class Video extends NotORM {

    /* 我的视频 */
    public function myVideo($uid,$status,$p) {
        
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;
		$where="uid={$uid} and status not in(2,3) ";
		if($status>0){
			$where.=" and status={$status}";
		}

		$list=\PhalApi\DI()->notorm->video
                    ->select('id,uid,title,thumb,thumb_s,href,likes,views,shares,status,isprivate,coin,reason,comments')
                    ->where($where)
                    ->order('id desc')
                    ->limit($start,$pnum)
                    ->fetchAll();

		return $list;
	}
    
    /* 视频列表 */
	public function getVideoList($where,$p,$order='id desc') {
        
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;

		$list=\PhalApi\DI()->notorm->video
                    ->select('id,uid,title,thumb,thumb_s,href,likes,views,shares,isprivate,coin,comments')
                    ->where('status=1')
                    ->where($where)
                    ->order($order)
                    ->limit($start,$pnum)
                    ->fetchAll();

		return $list;
	}

    /* 视频价格 */
    public function getFee() {

		$list=\PhalApi\DI()->notorm->video_fee
				->select('coin,level')
                ->order('coin asc')
				->fetchAll();

        return $list; 
    }
    
	/* 发布视频 */
	public function setVideo($data) {
		
		$rs=\PhalApi\DI()->notorm->video->insert($data);

		return $rs;
	}

	/* 更新 */
	public function upVideo($uid,$videoid,$data) {
		$rs=\PhalApi\DI()->notorm->video->where('id=? and uid=?',$videoid,$uid)->update($data);
		return $rs;
	}

	/* 获取视频信息 */
	public function getVideo($videoid,$where='status=1') {
		$info=\PhalApi\DI()->notorm->video
                ->select('id,uid,title,thumb,thumb_s,href,likes,views,shares,isprivate,coin,comments')
				->where('id = ?',$videoid)
                ->where($where)
				->fetchOne();
		return $info;
	}
    
    /* 观看 */
	public function addView($uid,$videoid) {
		$rs=\PhalApi\DI()->notorm->video
				->where("id = ? and uid!=?",$videoid,$uid)
				->update( array('views' => new \NotORM_Literal("views + 1") ) );
		return $rs;
	}
    
    /* 是否点赞 */
	public function islike($uid,$videoid) {
        
		$isexist=\PhalApi\DI()->notorm->video_like
						->select("id")
						->where("uid='{$uid}' and videoid='{$videoid}'")
						->fetchOne();
        if($isexist){
            return '1';
        }
        
		return '0';
	}
    
    /* 点赞+ */
	public function addLike($uid,$videoid) {
		$rs=\PhalApi\DI()->notorm->video
				->where("id = ?",$videoid)
				->update( array('likes' => new \NotORM_Literal("likes + 1") ) );
                
        \PhalApi\DI()->notorm->video_like
                    ->insert(array("uid"=>$uid,"videoid"=>$videoid,"addtime"=>time() ));
		return $rs;
	}
    
    /* 点赞- */
	public function reduceLike($uid,$videoid) {
		$rs=\PhalApi\DI()->notorm->video
                    ->where("id = '{$videoid}' and likes>0")
                    ->update( array('likes' => new \NotORM_Literal("likes - 1") ) );
        
        \PhalApi\DI()->notorm->video_like
                    ->where("uid='{$uid}' and videoid='{$videoid}'")
                    ->delete();
                            
		return $rs;
	}
    
    /* 分享 */
	public function addShare($uid,$videoid) {

		$rs=\PhalApi\DI()->notorm->video
				->where("id = '{$videoid}'")
				->update( array('shares' => new \NotORM_Literal("shares + 1") ) );
        
		return $rs;
	}

    /* 删除 */
	public function delVideo($videoid) {

		$rs=\PhalApi\DI()->notorm->video
				->where("id = '{$videoid}'")
				->delete();
        
        \PhalApi\DI()->notorm->video_like
				->where("videoid = '{$videoid}'")
				->delete();
                
        \PhalApi\DI()->notorm->video_view
				->where("videoid = '{$videoid}'")
				->delete();
                
        \PhalApi\DI()->notorm->video_report
				->where("videoid = '{$videoid}'")
				->delete();
        
		return $rs;
	}

    /* 举报类型 */
	public function getReport() {

		$list=\PhalApi\DI()->notorm->video_report_class
                ->select('name,name_en')
				->order('list_order asc')
				->fetchAll();
        
		return $list;
	}
    		
	/* 举报 */
	public function setReport($data) {
		
		$rs=\PhalApi\DI()->notorm->video_report->insert($data);

		return $rs;
	}
    
    /* 是否购买 */
	public function isbuy($uid,$videoid) {
        
		$isexist=\PhalApi\DI()->notorm->video_buy
						->select("id")
						->where("uid='{$uid}' and videoid='{$videoid}'")
						->fetchOne();
        if($isexist){
            return '1';
        }
        
		return '0';
	}

    /* 是否购买 */
	public function buyVideo($data) {
        
		$rs=\PhalApi\DI()->notorm->video_buy->insert($data);
        return $rs;
	}

	//获取首页推荐视频列表
	public function getRecommendVideos($uid,$p){
		if($p<1){
			$p=1;
		}

		$pnums=50;
        $start=($p-1)*$pnums;

		$where="status=1";
		if($p>1){
			$recommend_addtime=$_SESSION['recommend_addtime'];
			if($recommend_addtime){
				$where.=" and addtime <".$recommend_addtime;
			}
		}

		$list=\PhalApi\DI()->notorm->video
                ->select('id,uid,title,thumb,thumb_s,href,likes,views,shares,isprivate,coin,comments')
                ->where($where)
                ->order("addtime desc")
                ->limit($start,$pnums)
                ->fetchAll();

        $domain_vip = new Domain_Vip();
        $isvip=0;
        $vipinfo=$domain_vip->myVip($uid);
        if($vipinfo['isvip']){
            $isvip=1;
        }

        foreach ($list as $k => $v) {
        	$userinfo=\App\getUserInfo($v['uid']);
        	$v['avatar']=$userinfo['avatar'];
        	$v['user_nickname']=$userinfo['user_nickname'];
        	$v['userinfo']=$userinfo;
        	$v['thumb']=\App\get_upload_path($v['thumb']);
        	$v['thumb_s']=\App\get_upload_path($v['thumb_s']);
        	$v['href']=\App\get_upload_path($v['href']);
        	$v['likes']=\App\NumberFormat($v['likes']);
            $v['views']=\App\NumberFormat($v['views']);
            $v['shares']=\App\NumberFormat($v['shares']);
            $v['comments']=\App\NumberFormat($v['comments']);
            $v['cansee']='0';
            $v['isattent']=\App\isAttention($uid,$v['uid']);

            if($v['uid']==$uid){
                $v['islike']='0';
                $v['cansee']='1';
            }else{
            	$v['islike']=$this->islike($uid,$v['id']);
            
                if($v['isprivate']==1){
                    $isbuy=$this->isbuy($uid,$v['id']);
                    if($isbuy || $isvip){
                        $v['cansee']='1';
                    }
                }else{
                    $v['cansee']='1';
                }
            }

            $list[$k]=$v;
        }

        $last=end($list);
        if($last){
        	$_SESSION['recommend_addtime']=$last['addtime'];
        }

        return $list;
	}

	/* 评论/回复 */
    public function setComment($data) {
    	$videoid=$data['videoid'];

		/* 更新 视频 */
		\PhalApi\DI()->notorm->video
            ->where("id = '{$videoid}'")
		 	->update( array('comments' => new \NotORM_Literal("comments + 1") ) );
		
        \PhalApi\DI()->notorm->video_comments
            ->insert($data);
			
		$videoinfo=\PhalApi\DI()->notorm->video
					->select("comments")
					->where('id=?',$videoid)
					->fetchOne();
					
		$count=\PhalApi\DI()->notorm->video_comments
					->where("commentid='{$data['commentid']}'")
					->count();
		$rs=array(
			'comments'=>$videoinfo['comments'],
			'replys'=>$count,
		);

		return $rs;	
    }

    /* 评论列表 */
	public function getComments($uid,$videoid,$p){
        if($p<1){
            $p=1;
        }
		$nums=20;
		$start=($p-1)*$nums;
		$comments=\PhalApi\DI()->notorm->video_comments
					->select("*")
					->where("videoid='{$videoid}' and parentid='0'")
					->order("addtime desc")
					->limit($start,$nums)
					->fetchAll();
		

		foreach($comments as $k=>$v){
			$comments[$k]['userinfo']=\App\getUserInfo($v['uid']);				
			$comments[$k]['datetime']=\App\datetime($v['addtime']);	
			$comments[$k]['likes']=\App\NumberFormat($v['likes']);	
			if($uid){
				$comments[$k]['islike']=(string)$this->ifCommentLike($uid,$v['id']);	
			}else{
				$comments[$k]['islike']='0';	
			}
			
			if($v['touid']>0){
				$touserinfo=\App\getUserInfo($v['touid']);
			}
			if(!$touserinfo){
				$touserinfo=(object)array();
				$comments[$k]['touid']='0';
			}
			$comments[$k]['touserinfo']=$touserinfo;

			$count=\PhalApi\DI()->notorm->video_comments
					->where("commentid='{$v['id']}'")
					->count();
			$comments[$k]['replys']=$count;
            
            /* 回复 */
            $reply=\PhalApi\DI()->notorm->video_comments
					->select("*")
					->where("commentid='{$v['id']}'")
					->order("addtime desc")
					->limit(0,1)
					->fetchAll();
            foreach($reply as $k1=>$v1){
                
                $v1['userinfo']=\App\getUserInfo($v1['uid']);				
                $v1['datetime']=\App\datetime($v1['addtime']);	
                $v1['likes']=\App\NumberFormat($v1['likes']);	
                $v1['islike']=(string)$this->ifCommentLike($uid,$v1['id']);
                if($v1['touid']>0){
                    $touserinfo=\App\getUserInfo($v1['touid']);
                }
                if(!$touserinfo){
                    $touserinfo=(object)array();
                    $v1['touid']='0';
                }
                
                if($v1['parentid']>0 && $v1['parentid']!=$v['id']){
                    $tocommentinfo=\PhalApi\DI()->notorm->video_comments
                        ->select("content,at_info")
                        ->where("id='{$v1['parentid']}'")
                        ->fetchOne();
                }else{
                    $tocommentinfo=(object)array();
                    $touserinfo=(object)array();
                    $v1['touid']='0';
                }
                $v1['touserinfo']=$touserinfo;
                $v1['tocommentinfo']=$tocommentinfo;


                $reply[$k1]=$v1;
            }
            
            $comments[$k]['replylist']=$reply;
		}


		$commentnum=\PhalApi\DI()->notorm->video_comments
					->where("videoid='{$videoid}'")
					->count();
		
		$rs=array(
			"comments"=>$commentnum,
			"commentlist"=>$comments,
		);
		
		return $rs;
	}

	/* 评论/回复 是否点赞 */
	public function ifCommentLike($uid,$commentid){
		$like=\PhalApi\DI()->notorm->video_comments_like
				->select("id")
				->where("uid='{$uid}' and commentid='{$commentid}'")
				->fetchOne();
		if($like){
			return 1;
		}else{
			return 0;
		}	
	}

	/* 回复列表 */
	public function getReplys($uid,$commentid,$p){
        if($p<1){
            $p=1;
        }
		$nums=20;
		$start=($p-1)*$nums;
		$comments=\PhalApi\DI()->notorm->dynamics_comments
					->select("*")
					->where("commentid='{$commentid}'")
					->order("addtime desc")
					->limit($start,$nums)
					->fetchAll();

		
		foreach($comments as $k=>$v){
			$comments[$k]['userinfo']=\App\getUserInfo($v['uid']);				
			$comments[$k]['datetime']=\App\datetime($v['addtime']);	
			$comments[$k]['likes']=\App\NumberFormat($v['likes']);	
			$comments[$k]['islike']=(string)$this->ifCommentLike($uid,$v['id']);
			if($v['touid']>0){
				$touserinfo=\App\getUserInfo($v['touid']);
			}
			if(!$touserinfo){
				$touserinfo=(object)array();
				$comments[$k]['touid']='0';
			}

			if($v['parentid']>0 && $v['parentid']!=$commentid){
				$tocommentinfo=\PhalApi\DI()->notorm->dynamics_comments
					->select("content")
					->where("id='{$v['parentid']}'")
					->fetchOne();
			}else{
				$tocommentinfo=(object)array();
				$touserinfo=(object)array();
				$comments[$k]['touid']='0';
			}
			$comments[$k]['touserinfo']=$touserinfo;
			$comments[$k]['tocommentinfo']=$tocommentinfo;
		}
		return $comments;
	}

	/*删除评论 删除子级评论*/
	public function delComments($uid,$videoid,$commentid,$commentuid) {
       $result=\PhalApi\DI()->notorm->video
					->select("uid")
					->where("id='{$videoid}'")
					->fetchOne();	
					
		if(!$result){
			return 1001;
		}			
		
		
		if($uid!=$commentuid){
			if($uid!=$result['uid']){
				return 1002;
			}
		}
			
		
		
		// 删除 评论记录
		\PhalApi\DI()->notorm->video_comments
					->where("id='{$commentid}'")
					->delete(); 
		//删除视频评论喜欢
		\PhalApi\DI()->notorm->video_comments_like
					->where("commentid='{$commentid}'")
					->delete(); 
		/* 更新 视频 */
		\PhalApi\DI()->notorm->video
            ->where("id = '{$videoid}' and comments>0")
		 	->update( array('comments' => new \NotORM_Literal("comments - 1") ) );
		
		
		//删除相关的子级评论
		$lists=\PhalApi\DI()->notorm->video_comments
				->select("*")
				->where("commentid='{$commentid}' or parentid='{$commentid}'")
				->fetchAll();
		foreach($lists as $k=>$v){
			//删除 评论记录
			\PhalApi\DI()->notorm->video_comments
						->where("id='{$v['id']}'")
						->delete(); 
			//删除视频评论喜欢
			\PhalApi\DI()->notorm->video_comments_like
						->where("commentid='{$v['id']}'")
						->delete(); 
						
			/* 更新 视频 */
			\PhalApi\DI()->notorm->video
				->where("id = '{$v['videoid']}' and comments>0")
				->update( array('comments' => new \NotORM_Literal("comments - 1") ) );
		}
			
		
						
		return 0;

    }


    /* 评论/回复 点赞 */
	public function addCommentLike($uid,$commentid){
		$rs=array(
			'islike'=>'0',
			'likes'=>'0',
		);

		//根据commentid获取对应的评论信息
		$commentinfo=\PhalApi\DI()->notorm->video_comments
			->where("id='{$commentid}'")
			->fetchOne();

		if(!$commentinfo){
			return 1001;
		}

		$like=\PhalApi\DI()->notorm->video_comments_like
			->select("id")
			->where("uid='{$uid}' and commentid='{$commentid}'")
			->fetchOne();

		if($like){
			\PhalApi\DI()->notorm->video_comments_like
						->where("uid='{$uid}' and commentid='{$commentid}'")
						->delete();
			
			\PhalApi\DI()->notorm->video_comments
				->where("id = '{$commentid}' and likes>0")
				->update( array('likes' => new \NotORM_Literal("likes - 1") ) );
			$rs['islike']='0';

		}else{
			\PhalApi\DI()->notorm->video_comments_like
						->insert(array("uid"=>$uid,"commentid"=>$commentid,"addtime"=>time(),"touid"=>$commentinfo['uid'],"videoid"=>$commentinfo['videoid'] ));
			
			\PhalApi\DI()->notorm->video_comments
				->where("id = '{$commentid}'")
				->update( array('likes' => new \NotORM_Literal("likes + 1") ) );
			$rs['islike']='1';
		}	
		
		$video=\PhalApi\DI()->notorm->video_comments
				->select("likes")
				->where("id = '{$commentid}'")
				->fetchOne();

		//获取视频信息
		$videoinfo=\PhalApi\DI()->notorm->video->select("thumb")->where("id='{$commentinfo['videoid']}'")->fetchOne();

		$rs['likes']=$video['likes'];

		return $rs; 		
	}


}
